<template>
  <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-position="left" label-width="0px"
           v-if="islogin==true"
           class="demo-ruleForm login-container">
    <h3 class="title">系统登录</h3>
    <el-form-item prop="account">
      <el-input type="text" v-model="ruleForm2.account" auto-complete="off" placeholder="账号" v-on:change="menbername_change"></el-input>
    </el-form-item>
    <el-form-item prop="checkPass">
      <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="">
      <el-select v-model="ruleForm2.video_role_id" placeholder="请选择组织" auto-complete="off" style="width: 280px;">
        <el-option :label="item.role_name" :value="item.video_role_id" :key="item.video_role_id" v-for="item in roles"></el-option>
        <!--<el-option label="管理员" value="42f665ae96f04899a20bdb9cf06c1d65"></el-option>-->
      </el-select>
    </el-form-item>
    <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
    <a style="color: #3399ff;text-align: right;margin-left: 130px;" @click="islogin=false">新建用户</a>
    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit2" :loading="logining">登录
      </el-button>
      <!--<el-button @click.native.prevent="handleReset2">重置</el-button>-->
    </el-form-item>
  </el-form>

  <el-form :model="ruleForm3" :rules="rules3" ref="ruleForm3" label-position="left" label-width="80px"
           v-else="islogin==false"
           class="demo-ruleForm login-container" style="width: 400px;margin-top: 32px;">
    <h3 class="title">用户注册</h3>
    <el-form-item prop="member_name" label="账号">
      <el-input type="text" v-model="ruleForm3.member_name" auto-complete="off" placeholder="请输入账号"></el-input>
    </el-form-item>
    <el-form-item prop="member_password" label="密码">
      <el-input type="password" v-model="ruleForm3.member_password" auto-complete="off" placeholder="请输入密码"></el-input>
    </el-form-item>
    <el-form-item prop="member_password2" label="确认密码">
      <el-input type="password" v-model="ruleForm3.member_password2" auto-complete="off"
                placeholder="请输入确认密码"></el-input>
    </el-form-item>
    <el-form-item prop="mobile" label="电话">
      <el-input type="text" v-model="ruleForm3.mobile" auto-complete="off" placeholder="请输入电话"></el-input>
    </el-form-item>
    <el-form-item label="部门">
      <el-select v-model="ruleForm3.organization_id" placeholder="请选择部门" auto-complete="off">
        <el-option :label="item.label" :value="item.id" :key="item.id" v-for="item in orgdata"></el-option>
        <!--<el-option label="管理员" value="42f665ae96f04899a20bdb9cf06c1d65"></el-option>-->
      </el-select>
    </el-form-item>
    <el-form-item prop="member_comments" label="备注">
      <el-input type="textarea" v-model="ruleForm3.member_comments" auto-complete="off" placeholder="请输入备注"></el-input>
    </el-form-item>
    <el-form-item style="width:100%;">
      <!--<el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit3" :loading="logining">提交-->
      <!--</el-button>-->
      <el-form-item>
        <el-button type="primary" @click.native.prevent="handleSubmit3" :loading="logining">立即创建</el-button>
        <el-button @click="islogin=true">取消</el-button>
      </el-form-item>
    </el-form-item>
  </el-form>
</template>

<script>
  import {requestLogin, adduser, getORGlist,getRoleByname} from '../api/api';
  //import NProgress from 'nprogress'
  export default {
    data() {
      return {
        islogin: true,
        logining: false,
        ruleForm2: {
          video_member_id: '',
          account: 'jackson',
          checkPass: '123',
          video_role_id: ''
        },
        rules2: {
          account: [
            {required: true, message: '请输入账号', trigger: 'blur'},
            //{ validator: validaePass }
          ],
          checkPass: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            //{ validator: validaePass2 }
          ],
          video_role_id: [
            {required: true, message: '请选择组织', trigger: 'blur'},
            //{ validator: validaePass2 }
          ]
        },
        checked: true,
        ruleForm3: {
          member_name: '',
          member_password: '',
          member_password2: '',
          mobile: '',
          member_comments: '',
          organization_id: ''
        },
        rules3: {
          member_name: [
            {required: true, message: '请输入账号', trigger: 'blur'},
            //{ validator: validaePass }
          ],
          member_password: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            //{ validator: validaePass2 }
          ],
          member_password2: [
            {required: true, message: '请输入确认密码', trigger: 'blur'},
            //{ validator: validaePass2 }
          ],
          mobile: [
            {required: true, message: '请输入电话,不能大于11位', max: 11, trigger: 'blur'},
            //{ validator: validaePass2 }
          ],
          member_comments: [
            {required: true, message: '请输入备注', trigger: 'blur'},
            //{ validator: validaePass2 }
          ],
          organization_id: [
            {required: true, message: '请选择部门', trigger: 'blur'},
            //{ validator: validaePass2 }
          ]
        },
        orgdata: [],
        roles: []
      };
    },
    methods: {
      menbername_change(value){
        this.getRoles();
        console.log(value);
      },
      handleReset2() {
        this.$refs.ruleForm2.resetFields();
      },
      handleSubmit2(ev) {
        if(this.ruleForm2.video_role_id==''){
          return  this.$message({
            type: 'error',
            message: '请选择组织'
          });
        }
        var _this = this;
        this.$refs.ruleForm2.validate((valid) => {
          if (valid) {
            this.logining = true;
            var loginParams = {member_name: this.ruleForm2.account, member_password: this.ruleForm2.checkPass,video_role_id:this.ruleForm2.video_role_id};
            var jsonstr = JSON.stringify(loginParams);
            requestLogin(jsonstr)
              .then(responsedata => {
                _this.logining = false;
                //NProgress.done();
                let {status, data} = responsedata;
                if (status !== 200) {
                  this.$message({
                    message: '请求失败',
                    type: 'error'
                  });
                } else {
                  var descritpion = data.descritpion;
                  if (data.status == '90000') {
                    var user = data.values[0];
                    this.video_member_id = user.video_member_id;
                    user.member_password =this.ruleForm2.checkPass;
                    sessionStorage.setItem('user', JSON.stringify(user));
                    this.base.setPermission();
                    this.$router.push({path: '/member_role', query: {name: this.ruleForm2.account}});
                  } else {
                    this.$message({
                      type: 'error',
                      message: descritpion
                    });
                  }

                }
              })
              .catch(function (response) {
                _this.logining = false;
                _this.$notify.error({
                  title: '错误',
                  message: '网络请求失败'
                });
                console.log(response);
              });
          } else {
            console.log('error submit!!');
            return false;
          }
        });
        //NProgress.start();
      },
      handleSubmit3(ev) {
        var _this = this;
        this.$refs.ruleForm3.validate((valid) => {
          if (valid) {
            if (this.ruleForm3.member_password != this.ruleForm3.member_password2) {
              return this.$message({
                type: 'error',
                message: '密码不一致'
              });
            }
            this.logining = true;
            var addloginParams = {
              member_name: this.ruleForm3.member_name,
              member_password: this.ruleForm3.member_password,
              member_comments: this.ruleForm3.member_comments,
              mobile: this.ruleForm3.mobile,
              organization_id: this.ruleForm3.organization_id
            };
            adduser(addloginParams)
              .then(responsedata => {
                _this.logining = false;
                //NProgress.done();
                let {status, data} = responsedata;
                if (status !== 200) {
                  this.$message({
                    message: '请求失败',
                    type: 'error'
                  });
                } else {
                  var descritpion = data.descritpion;
                  if (data.status == '90000') {
//                    var user = data.values[0];
//                    sessionStorage.setItem('user', JSON.stringify(user));
//                    this.$router.push({path: '/VideoPlayer'});
                    this.ruleForm2.account = this.ruleForm3.member_name;
                    this.ruleForm2.checkPass = this.ruleForm3.member_password;
                    this.islogin = true;
                    this.$message({
                      type: 'success',
                      message: '提交成功'
                    });
                  } else {
                    this.$message({
                      type: 'error',
                      message: descritpion
                    });
                  }

                }
              })
              .catch(function (response) {
                _this.logining = false;
                _this.$notify.error({
                  title: '错误',
                  message: '网络请求失败'
                });
                console.log(response);
              });

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      initDatesousre() {
        var _this = this;
        var loginParams = {};
        var jsonstr = JSON.stringify(loginParams);
        getORGlist(jsonstr)
          .then(responsedata => {
            _this.logining = false;
            //NProgress.done();
            let {status, data} = responsedata;
            if (status !== 200) {
              this.$message({
                message: '请求失败',
                type: 'error'
              });
            } else {
              var orgList = data.values;
              _this.orgdata = orgList;
            }
          })
          .catch(function (response) {
            _this.logining = false;
            _this.$notify.error({
              title: '错误',
              message: '网络请求失败'
            });
            console.log(response);
          });
      },
      getRoles() {
        var _this = this;
        var Params = { member_name: this.ruleForm2.account};
        var jsonstr = JSON.stringify(Params);
        getRoleByname(jsonstr)
          .then(responsedata => {
            _this.logining = false;
            //NProgress.done();
            let {status, data} = responsedata;
            if (status !== 200) {
              this.$message({
                message: '请求失败',
                type: 'error'
              });
            } else {
              this.roles = data.values;
            }
          })
          .catch(function (response) {
            _this.logining = false;
            _this.$notify.error({
              title: '错误',
              message: '网络请求失败'
            });
            console.log(response);
          });
      },
    },
    mounted() {
      this.initDatesousre();
      this.getRoles();
    }
  }

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 120px auto;
    vertical-align: middle;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
  }

  .title {
    margin: 0px auto 40px auto;
    text-align: center;
    color: #505458;
  }

  .remember {
    margin: 0px 0px 35px 0px;

  }
</style>
